package com.zgjkhis.modules.outpatient.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.github.pagehelper.PageInfo;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.dozer.service.IGenerator;
import com.zgjkhis.modules.enums.SexEnum;
import com.zgjkhis.modules.enums.WhetherEnum;
import com.zgjkhis.modules.member.service.MemberService;
import com.zgjkhis.modules.outpatient.domain.Account;
import com.zgjkhis.modules.outpatient.dto.AccountDto;
import com.zgjkhis.modules.outpatient.dto.AccountLabelDto;
import com.zgjkhis.modules.outpatient.dto.AccountQueryCriteria;
import com.zgjkhis.modules.outpatient.service.AccountLabelService;
import com.zgjkhis.modules.outpatient.service.AccountService;
import com.zgjkhis.modules.outpatient.service.mapper.AccountMapper;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.DateUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yzq
 * @Description 患者
 * @Date 2021/9/14 16:12
 * @Param
 * @return
 **/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccountServiceImpl extends BaseServiceImpl<AccountMapper, Account> implements AccountService {

    private final IGenerator generator;
    private final AccountLabelService accountLabelService;
    private final MemberService memberService;

    public AccountServiceImpl(IGenerator generator, AccountLabelService accountLabelService, MemberService memberService) {
        this.generator = generator;
        this.accountLabelService = accountLabelService;
        this.memberService = memberService;
    }

    @Override
    public AccountDto findDetailById(String id) {
        String pharmacyId= LoginUserUtil.getPharmId();
        AccountDto account = baseMapper.findDetailById(id, pharmacyId);
        if(!"0".equals(account.getMembershipType())){
            account.setMember(memberService.getById(account.getMembershipType()));
        }
        return  account;
    }

    @Override
    public PageResult<AccountDto> findAllByPageable(AccountQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<AccountDto> page = new PageInfo<>(baseMapper.findAllByPageable(criteria));
        for (AccountDto accountDto:page.getList()) {
            AccountDto temp = baseMapper.findContentById(accountDto.getId());
            accountDto.setTotalFee(temp.getTotalFee());
            accountDto.setJzCount(temp.getJzCount());
            List<AccountLabelDto> list=accountLabelService.findListByAccountId(accountDto.getId());
            accountDto.setLabelDtos(list);
        }
        return PageResult.data(page, AccountDto.class);
    }

    @Override
    public void batchSave(MultipartFile file) throws Exception {
        ExcelReader excelReader= ExcelUtil.getReader(file.getInputStream());
        List<List<Object>> listAll = excelReader.read();
        List<Account> list=new ArrayList<>();
        for (int i=1;i<listAll.size();i++) {
            Account account=new Account();
            List<Object> rowlist=listAll.get(i);
            Object name=rowlist.get(0);
            Object sex=rowlist.get(1);
            Object birthday=rowlist.get(2);
            if(!CharUtil.isEmpty(name) && !CharUtil.isEmpty(sex) && !CharUtil.isEmpty(birthday)){
                account.setName(name.toString());
                account.setSex(SexEnum.findByValue(sex.toString()));
                account.setBirthday(DateUtils.dateTime("yyyy-MM-dd",birthday.toString()));
                account.setAge(DateUtils.getAge(account.getBirthday()));
                account.setPhone(CharUtil.isEmpty(rowlist.get(3))?"":rowlist.get(3).toString());
                account.setIdCard(CharUtil.isEmpty(rowlist.get(4))?"":rowlist.get(4).toString());
                account.setMaritalStatus(CharUtil.isEmpty(rowlist.get(5))?"":rowlist.get(5).toString());
                account.setNation(CharUtil.isEmpty(rowlist.get(6))?"":rowlist.get(6).toString());
                account.setAddress(CharUtil.isEmpty(rowlist.get(7))?"":rowlist.get(7).toString());
                account.setIsChronicDisease(CharUtil.isEmpty(rowlist.get(8))?0: WhetherEnum.findByValue(rowlist.get(8).toString()));
                account.setIllness(CharUtil.isEmpty(rowlist.get(9))?"":rowlist.get(9).toString());
                list.add(account);
            }
        }
        this.saveBatch(list);
    }
}
